/**
 * company mapper
 * @author wuji
 * @date 2020-07-19
 */
const {db} = require("../db/db.js");

/**
 * Select All Company
 */
exports.selectAllCompany = function(cb) {
    db.all("select id, name, orders from tb_company order by orders", cb);
}

/**
 * Select Company By Name
 * @param {*} companyName 
 */
exports.selectCompanyIdByName = function(name, cb) {
    db.all("select id from tb_company where name = ?", name, cb);
}

/**
 * Select Company By Id
 * @param {*} companyId 
 */
exports.selectCompanyById = function(id, cb) {
    db.all("select id, name, orders from tb_company where id = ?", id, cb);
}

/**
 * Add Company
 * @param {*} company 
 */
exports.addCompany = function(company, cb) {
    db.run("insert into tb_company (name, orders) values (?,?)",
        [company.name, company.orders], cb);
}

/**
 * Delete Company By Id
 * @param {*} companyId 
 */
exports.deleteCompanyById = function(companyId, cb) {
    db.run("delete from tb_company where id = ?", companyId, cb);
}

/**
 * Update Company
 * @param {*} company 
 */
exports.updateCompany = function(company, cb) {
    db.run("update tb_company set name = ?, orders = ? where id = ?", 
        [company.name, company.orders, company.id], cb);
}